/**
 * Copyright (c) 2012 Axa Holding Belgium, SA. All rights reserved.
 * This software is the confidential and proprietary information of the AXA Group.
 */
package chabernac.android.wordbattle.paint.classic;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.view.View;
import chabernac.android.wordbattle.WordBattleView;
import chabernac.android.wordbattle.model.Letter;
import chabernac.android.wordbattle.model.WordRaster;
import chabernac.android.wordbattle.paint.iDrawable;

public class WordRasterDrawable extends iDrawable {
  private WordBattleView myWordBattleView;
  private final WordRaster myWordRaster;
  private final Paint myPaint = new Paint();
  
  private boolean isDebug = false;
  
  public WordRasterDrawable( WordRaster aWordRaster ) {
    super();
    myWordRaster = aWordRaster;
    
    init();
  }
  
  private void init(){
    myPaint.setStyle(Style.FILL);
    myPaint.setColor(Color.BLACK);
    myPaint.setAntiAlias(true);
  }

  @Override
  public void paint( Canvas aCanvas, View aView ) {
    myWordBattleView = (WordBattleView)aView;
    
    float theMinSize = (float)(Math.min(myWordBattleView.getHeightPerRow(), myWordBattleView.getWidthPerColumn()) * 0.8 );
    myPaint.setTextSize(theMinSize);
    
    Rect theFontBounds = new Rect();
    
    for(int x=0;x<myWordRaster.getColumns();x++){
      for(int y=0;y<myWordRaster.getRows();y++){
        Letter theLetter = myWordRaster.getLetter(x, y);
        
        myPaint.getTextBounds(theLetter.getLetter(), 0 , 1 , theFontBounds);
        
        float theFontWidth = theFontBounds.width();
        float theFontHeight = theFontBounds.height();
        
        float theXOffset = (myWordBattleView.getWidthPerColumn() - theFontWidth) / 2F;
        float theYOffset = (myWordBattleView.getHeightPerRow() - theFontHeight) / 2F;
        
        float theXCell = WordBattleView.BORDER_SIZE + x * myWordBattleView.getWidthPerColumn();
        float theYCell = WordBattleView.BORDER_SIZE + y * myWordBattleView.getHeightPerRow();
        
        float theX = theXCell + theXOffset;
        float theY = theYCell + theYOffset + theFontHeight;
        
        if(isDebug ){
          myPaint.setStyle(Style.FILL);
          myPaint.setColor(Color.YELLOW);

          float theCenterX = theXCell + myWordBattleView.getWidthPerColumn() / 2;
          float theCenterY = theYCell + myWordBattleView.getHeightPerRow() / 2;
          aCanvas.drawCircle(theCenterX, theCenterY, myWordBattleView.getSelectionRadius(), myPaint);
          
          myPaint.setStyle(Style.STROKE);
          myPaint.setStrokeWidth(2);
          myPaint.setColor(Color.RED);
          aCanvas.drawCircle(WordBattleView.BORDER_SIZE + x * myWordBattleView.getWidthPerColumn(), WordBattleView.BORDER_SIZE + y * myWordBattleView.getHeightPerRow(), 2, myPaint);
          myPaint.setColor(Color.GREEN);
          aCanvas.drawRect(theX, theY, theX + theFontWidth, theY - theFontHeight, myPaint);
          myPaint.setColor(Color.RED);
          aCanvas.drawCircle(theX, theY, 2, myPaint);
        }
          
        myPaint.setColor(Color.BLACK);
        aCanvas.drawText(theLetter.getLetter(), theX - theFontBounds.left, theY - theFontBounds.bottom , myPaint);
      }
    }

  }

}
